/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { generateId } from '../../../../common/util/math-util.js';
import { Priority } from '../enum/priority.js';

/**
 * 工作单元
 * 外部向池提交的逻辑实体
 *
 * @since 2022/8/12
 */
export class Work {
  private readonly id: string;
  private priority: Priority;
  private readonly targetWorkers: number[] | undefined;
  private readonly workPath: string;
  private readonly inputData: unknown;
  private readonly sideEffects: boolean;
  private readonly preludeDeps: string[];
  private readonly memorySensitive: boolean;

  constructor(
    priority: Priority,
    targetWorkers: number[] | undefined,
    workPath: string,
    inputData?: unknown,
    sideEffects = false,
    preludeDeps: string[] = ['@ohos/hvigor'],
    memorySensitive = false,
  ) {
    this.id = generateId();
    this.priority = priority;
    this.workPath = workPath;
    this.targetWorkers = targetWorkers;
    this.inputData = inputData;
    this.sideEffects = sideEffects;
    this.preludeDeps = preludeDeps;
    this.memorySensitive = memorySensitive;
  }

  /**
   * 获取id
   *
   * @returns {string} 工作单元id
   */
  getId(): string {
    return this.id;
  }

  /**
   * 获取优先级
   *
   * @returns {Priority} 工作单元优先级
   */
  getPriority(): Priority {
    return this.priority;
  }

  /**
   * 获取工作路径
   *
   * @returns {string} 工作单元路径
   */
  getWorkPath(): string {
    return this.workPath;
  }

  /**
   * 获取输入数据
   *
   * @returns {unknown} 工作单元输入数据
   */
  getInputData(): unknown {
    return this.inputData;
  }

  /**
   * 是否会污染线程环境
   *
   * @returns {boolean} 判断结果
   */
  hasSideEffects(): boolean {
    return this.sideEffects;
  }

  /**
   * 获取目标worker集合
   *
   * @returns {number[] | undefined} 工作单元目标worker集合
   */
  getTargetWorkers(): number[] | undefined {
    return this.targetWorkers;
  }

  /**
   * 设置优先级
   *
   * @param priority 待设置的优先级
   */
  setPriority(priority: Priority): void {
    this.priority = priority;
  }

  /**
   * 获取预热线程时需要require的依赖列表
   *
   * @returns {string[]} 依赖列表
   */
  getPreludeDeps(): string[] {
    return this.preludeDeps;
  }

  /**
   * 是否内存敏感线程
   *
   * @returns {boolean} 判断结果
   */
  isMemorySensitive(): boolean {
    return this.memorySensitive;
  }
}
